# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
    scipen = 999,
    digits = 16,
    max.print = .Machine$integer.max,
    show.error.locations = TRUE,
    warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------
library(data.table)
library(ggplot2)
library(scales)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1L)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# READ IN OUR DATA -------------------------------------------------------------
# Focus on the aggregate, Q1, and Q4 estimates across time aggregations

db_w2_wages_dt <-
    readRDS("~/estimation-output/wealth_effect_estimates_db_w2_wages.rds")
setnames(db_w2_wages_dt, "ref_event_time", "event_time")
db_w2_wages_dt <-
    db_w2_wages_dt[event_time >= 97 & income_quartile %in% c(1, 4, 5)]

per_adult_total_empl_income_dt <-
    readRDS("~/estimation-output/wealth_effect_estimates_per_adult_total_empl_income.rds") #nolint
setnames(per_adult_total_empl_income_dt, "ref_event_time", "event_time")
per_adult_total_empl_income_dt <-
    per_adult_total_empl_income_dt[
        event_time >= 97 & income_quartile %in% c(1, 4, 5)
    ]

# Produce graph labels

db_w2_wages_dt[, sample_label := as.character(NA)]
db_w2_wages_dt[income_quartile == 5, sample_label := "Full Sample"]
db_w2_wages_dt[income_quartile == 1, sample_label := "Quartile 1"]
db_w2_wages_dt[income_quartile == 4, sample_label := "Quartile 4"]
db_w2_wages_dt[
    ,
    sample_label :=
        factor(
            sample_label,
            levels = c("Full Sample", "Quartile 1", "Quartile 4")
        )
]

per_adult_total_empl_income_dt[, sample_label := as.character(NA)]
per_adult_total_empl_income_dt[
    income_quartile == 5, sample_label := "Full Sample"
]
per_adult_total_empl_income_dt[
    income_quartile == 1, sample_label := "Quartile 1"
]
per_adult_total_empl_income_dt[
    income_quartile == 4, sample_label := "Quartile 4"
]
per_adult_total_empl_income_dt[
    ,
    sample_label :=
        factor(
            sample_label,
            levels = c("Full Sample", "Quartile 1", "Quartile 4")
        )
]

db_w2_wages_dt[, time_window_label := as.character(NA)]
db_w2_wages_dt[event_time == 99, time_window_label := "Avg (+1 to +5)"]
db_w2_wages_dt[event_time == 97, time_window_label := "Short Run (+1 to +2)"]
db_w2_wages_dt[event_time == 98, time_window_label := "Long Run (+3 to +5)"]
db_w2_wages_dt[
    ,
    time_window_label :=
        factor(
            time_window_label,
            levels = c("Avg (+1 to +5)", "Short Run (+1 to +2)", "Long Run (+3 to +5)") #nolint
        )
]

per_adult_total_empl_income_dt[, time_window_label := as.character(NA)]
per_adult_total_empl_income_dt[
    event_time == 99, time_window_label := "Avg (+1 to +5)"
]
per_adult_total_empl_income_dt[
    event_time == 97, time_window_label := "Short Run (+1 to +2)"
]
per_adult_total_empl_income_dt[
    event_time == 98, time_window_label := "Long Run (+3 to +5)"
]
per_adult_total_empl_income_dt[
    ,
    time_window_label :=
        factor(
            time_window_label,
            levels = c("Avg (+1 to +5)", "Short Run (+1 to +2)", "Long Run (+3 to +5)") #nolint
        )
]

# Figure B.5 -- wealth effects across time and by pre-win income

# Just to make it readable against to first, dark bar
errorbar_palette <- c("#999999",
                      "#000000",
                      "#000000")

figure_b_5_a <-
    ggplot(
        data = db_w2_wages_dt,
        aes(
            x = factor(time_window_label),
            y = (estimate * 100),
            fill = factor(sample_label)
        )
    ) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_errorbar(aes(
        ymin = ((estimate - (1.64 * cluster_se)) * 100),
        ymax = ((estimate + (1.64 * cluster_se)) * 100),
        color = factor(sample_label)
    ),
    position = position_dodge(0.9),
    show.legend = FALSE,
    width = 0.2
    ) +
    theme_bw(base_size = 12) +
    theme(
        panel.grid.minor = element_blank(), # remove actual gridlines
        panel.grid.major = element_blank()
    ) +
    scale_y_continuous(breaks = (seq.int(from = -35, to = 0, by = 5) / 10)) +
    theme(
        legend.position = "bottom",
        legend.title = element_blank(),
        legend.key.size = unit(1.75, "lines")
    ) +
    labs(x = "Timing", y = "Effect per $100 of Lottery Winnings") +
    geom_vline(xintercept = 1.5, linetype = 3) +
    scale_fill_viridis_d() +
    scale_color_manual(values = errorbar_palette) +
    coord_cartesian(ylim = c(-3.5, 0))

ggsave(
    plot = figure_b_5_a,
    filename = "~/paper/figures/figure-B.5a.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_b_5_a,
    filename = "~/paper/figures/figure-B.5a.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

figure_b_5_b <-
    ggplot(
        data = per_adult_total_empl_income_dt,
        aes(
            x = factor(time_window_label),
            y = (estimate * 100),
            fill = factor(sample_label)
        )
    ) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_errorbar(aes(
        ymin = ((estimate - (1.64 * cluster_se)) * 100),
        ymax = ((estimate + (1.64 * cluster_se)) * 100),
        color = factor(sample_label)
    ),
    position = position_dodge(0.9),
    show.legend = FALSE,
    width = 0.2
    ) +
    theme_bw(base_size = 12) +
    theme(
        panel.grid.minor = element_blank(), # remove actual gridlines
        panel.grid.major = element_blank()
    ) +
    scale_y_continuous(breaks = (seq.int(from = -35, to = 0, by = 5) / 10)) +
    theme(
        legend.position = "bottom",
        legend.title = element_blank(),
        legend.key.size = unit(1.75, "lines")
    ) +
    labs(x = "Timing", y = "Effect per $100 of Lottery Winnings") +
    geom_vline(xintercept = 1.5, linetype = 3) +
    scale_fill_viridis_d() +
    scale_color_manual(values = errorbar_palette) +
    coord_cartesian(ylim = c(-3.5, 0))

ggsave(
    plot = figure_b_5_b,
    filename = "~/paper/figures/figure-B.5b.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_b_5_b,
    filename = "~/paper/figures/figure-B.5b.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

# Housekeeping
db_w2_wages_dt <- NULL
per_adult_total_empl_income_dt <- NULL
errorbar_palette <- NULL
figure_b_5_a <- NULL
figure_b_5_b <- NULL
rm(
    db_w2_wages_dt,
    per_adult_total_empl_income_dt,
    errorbar_palette,
    figure_b_5_a,
    figure_b_5_b
)